package com.fan.shop.businesslogic;

import java.math.BigDecimal;

import org.apache.log4j.Logger;

import com.fan.shop.domain.PurchaseLine;
import com.fan.shop.domain.PurchaseLineId;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.PurchaseLineService;

public class PurchaseOrderLineCreateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(PurchaseOrderLineCreateProcess.class);
	private PurchaseLineService purchaseLineService;
	private String poNumber;
	private String itemCode;
	private BigDecimal price;
	private int qty;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PurchaseOrderLineCreateProcess.logger = logger;
	}

	public PurchaseLineService getPurchaseLineService() {
		return purchaseLineService;
	}

	public void setPurchaseLineService(PurchaseLineService purchaseLineService) {
		this.purchaseLineService = purchaseLineService;
	}

	public String getPoNumber() {
		return poNumber;
	}

	public void setPoNumber(String poNumber) {
		this.poNumber = poNumber;
	}

	public String getItemCode() {
		return itemCode;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	@Override
	public Integer processImpl() {
		if(purchaseLineService.hasPurchaseLine(poNumber, itemCode))
		{
			setErrorMessage("Purchase Order Line: " + poNumber + " " + itemCode + " already exists!");
			return ShopGlobalVariables.FAILED;
		}
		PurchaseLine purchaseLine = new PurchaseLine();
		PurchaseLineId purchaseLineId = new PurchaseLineId();
		purchaseLineId.setPoNumber(poNumber);
		purchaseLineId.setItemCode(itemCode);
		purchaseLine.setId(purchaseLineId);
		purchaseLine.setPrice(price);
		purchaseLine.setQuantity(qty);
		try {
			purchaseLineService.addPurchaseLine(purchaseLine);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage(e.getMessage());
			return ShopGlobalVariables.FAILED;
		}

		return ShopGlobalVariables.SUCCESS;
	}

}
